$(function() {
	//初始化
	Begin();
})
//定义邮箱是否测试通过变量，默认为否
var isTestEmailPass = true;

function resetAllInfo(){
	var url = "/system/loginSet/query";
	VanyAjax(url).done(function(data) {
		userLoginSet.userLoginSetForm.errorCount = data["errorCount"];
		userLoginSet.userLoginSetForm.limitLoginTime = data["limitLoginTime"];
		userLoginSet.inboxForm.sendServer = data["sendServer"];
		userLoginSet.inboxForm.sendServerInputCT = data["sendServer"].length;
		userLoginSet.inboxForm.port = data["port"];
		userLoginSet.inboxForm.portInputCT = data["port"].length;
		userLoginSet.inboxForm.sendUserName = data["sendUserName"];
		userLoginSet.inboxForm.sendUserNameInputCT = data["sendUserName"].length;
		userLoginSet.inboxForm.sendPassword = data["sendPassword"];
		userLoginSet.inboxForm.sendPasswordInputCT = data["sendPassword"].length;
		userLoginSet.inboxForm.testEmail = data["testEmail"];
		userLoginSet.inboxForm.testEmailInputCT = data["testEmail"].length;
		userLoginSet.inboxForm.findPwdContent = data["findPwdContent"];
	}, false, self);
}

var thisTabNum = "0";
//邮箱验证正则表达式
var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
var userLoginSet = new Vue({
	el: "#userLoginSet",
	data: function() {
		//自定义验证，验证机构编码
		var errorCount = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入登录错误次数'));
			}
			return callback();
		};
		var limitLoginTime = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入限制登录时长'));
			}
			return callback();
		};
		var sendServer = function(rule, value, callback) {
			passwordFirst = value;
			if(!value) {
				return callback(new Error('请输入发件邮箱服务器'));
			}
			return callback();
		};
		var port = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入发件邮箱端口'));
			}
			return callback();
		};
		var sendUserName = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入发件邮箱用户名'));
			}
			return callback();
		};
		var sendPassword = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入发件邮箱密码'));
			}
			return callback();
		};
		var testEmail = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入测试收件箱'));
			}
			if(!myreg.test(value)){
				return callback(new Error('邮箱格式不正确'));
			}
			return callback();
		};
		var findPwdContent = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入找回密码内容'));
			}
			return callback();
		};
		return {
			userLoginSetForm: {
				errorCount:'3',
				limitLoginTime:'20'
			},
			inboxForm:{
				sendServerInputCT: 0,
				sendServer:'',
				portInputCT: 0,
				port:'',
				sendUserNameInputCT: 0,
				sendUserName:'',
				sendPasswordInputCT: 0,
				sendPassword:'',
				testEmailInputCT: 0,
				testEmail:'',
				findPwdContent:'${uName}您好\r\n感谢您使用统一信息平台密码找回功能，\r\n您的新登录密码为：${resetPwd},为了您的账户安全，请登录后及时修改密码！'
			},
			rules: {
				errorCount: [{
					required: true,
					validator: errorCount,
					trigger: 'blur'
				}],
				limitLoginTime: [{
					required: true,
					validator: limitLoginTime,
					trigger: 'blur'
				}],
				sendServer: [{
					required: true,
					validator: sendServer,
					trigger: 'blur'
				}],
				port: [{
					required: true,
					validator: port,
					trigger: 'blur'
				}],
				sendUserName: [{
					required: true,
					validator: sendUserName,
					trigger: 'blur'
				}],
				sendPassword: [{
					required: true,
					validator: sendPassword,
					trigger: 'blur'
				}],
				testEmail: [{
					required: true,
					validator: testEmail,
					trigger: 'blur'
				}],
				findPwdContent: [{
					required: true,
					validator: findPwdContent,
					trigger: 'blur'
				}]
			},
			dialogVisible: false,
		}
	},
	methods: {
		descInput: function(inputName) {
			if(inputName == "sendServer") {
				this.inboxForm.sendServerInputCT = this.inboxForm.sendServer.length;
			}
			if(inputName == "port") {
				this.inboxForm.portInputCT = this.inboxForm.port.length;
			}
			if(inputName == "sendUserName") {
				this.inboxForm.sendUserNameInputCT = this.inboxForm.sendUserName.length;
			}
			if(inputName == "sendPassword") {
				this.inboxForm.sendPasswordInputCT = this.inboxForm.sendPassword.length;
			}
			if(inputName == "testEmail") {
				this.inboxForm.testEmailInputCT = this.inboxForm.testEmail.length;
			}
			
		},
		resetForm: function(formName) {
			//this.$refs['userLoginSetForm'].resetFields();
			//this.$refs['inboxForm'].resetFields();
			resetAllInfo();
		},
		testEmial: function(formName) {
			var self = this;
			var testEmailValue = self.inboxForm['testEmail'];
			this.$refs["inboxForm"].validate(function(valid) {
				if(!valid) {
					return false;
				} else {
					isTestEmailPass = true;
				}
			});
		},
		submitForm: function(formName) {
			if(!isTestEmailPass){
				this.$message('请先测试邮箱通过后再保存信息！');
			}else{
				var isPass = false;
				this.$refs["userLoginSetForm"].validate(function(valid) {
					if(!valid) {
						return false;
					} else {
						isPass = true;
					}
				});
				if(isPass){
					this.$refs["inboxForm"].validate(function(valid) {
						if(!valid) {
							return false;
						} else {
							userLoginSet.dialogVisible = true;
						}
					});
				}
			}
		},
		tijiao:function(obj) {
			this.dialogVisible = false;
			var self = this;
			var params =$.extend(self.inboxForm, self.userLoginSetForm);
			if(obj) {
				var url = "/system/loginSet/update";
				VanyAjax(url, params).done(function(data) {
					
				}, true, self);
			}
		}
	}
	
});

resetAllInfo();
//初始化
function Begin() {
	//面包屑
	new Vue().$mount('#crumbs');
	//左菜单滚动
	var oTop = $(".vany_authority").offset().top + 14;
	var sTop = 0;
	$(".vany_authority").scroll(function() {
		sTop = $(this).scrollTop();
		if(sTop >= oTop) {
			$(".conRight>div").css({
				"position": "fixed",
				"top": "60px",
				"right": "38px"
			});
		} else {
			$(".conRight>div").css({
				"position": "absolute",
				"top": "10px",
				"right": "10px"
			});
		}
	});

	sessionStorage.Jurisdiction = ""
}